package net.dfrz.oa.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;

import net.dfrz.oa.action.support.BaseAction;
import net.dfrz.oa.dto.UseOfficeDto;
import net.dfrz.oa.pojo.Office;
import net.dfrz.oa.pojo.UseOffice;
import net.dfrz.oa.pojo.User;
import net.dfrz.oa.service.OfficeService;
import net.dfrz.oa.service.UseOfficeService;
import net.dfrz.oa.service.UserService;
import net.dfrz.oa.utils.PageBean;
import net.dfrz.oa.viewvo.UseOfficeVo;

/**
 * 办公用品使用action
 * @author vovh
 *
 */
@Controller
@Scope("prototype")
@Namespace("")
@Results({
		@Result(name="useoffice",location="useoffice.jsp"),
		@Result(name="useofficesList", type="json", params={"root","jsonMap"})
})
public class UseOfficeAction extends BaseAction<UseOffice>{

	private UseOffice useOffice;
	
	private UseOfficeVo useOfficeVo;
	
	private UseOfficeDto useOfficeDto;
	
	@Autowired
	private UseOfficeService useOfficeService;
	@Autowired
	private UserService userService;
	@Autowired
	private OfficeService officeService;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//分页 -----
	private PageBean<UseOffice> pageList = new PageBean<UseOffice>();
	Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
	private String rows;//每页显示的记录数 
    private String page;//当前第几页 
    private List<UseOffice> list = null; //存放当前页数据
	private List<UseOfficeVo> volist = new ArrayList<UseOfficeVo>();//存放当前页vo数据
	@Override
	public UseOffice getModel() {
		useOffice = new UseOffice();
		return useOffice;
	}

	public PageBean<UseOffice> getPageList() {
		return pageList;
	}

	public void setPageList(PageBean<UseOffice> pageList) {
		this.pageList = pageList;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	
	public UseOfficeDto getUseOfficeDto() {
		return useOfficeDto;
	}

	public void setUseOfficeDto(UseOfficeDto useOfficeDto) {
		this.useOfficeDto = useOfficeDto;
	}

	/**
	 * 办公用品领用管理初始界面
	 * @return
	 */
	@Action("useoffice_useofficeManage")
	public String useofficeManage(){
		return "useoffice";
	}
	
	/**
	 * 分页查询
	 */
	@Action("useoffice_queryPageList")
	public String queryPageList(){
		//当前页   
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数  
        int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
        pageList.setPageNo(intPage);
        pageList.setPageSize(number);
        //spring-page 分页结果
        Page<UseOffice> page = null;
        //是否为条件查询判断
        String queryType = (String) getRequest().getParameter("type");
        if("usename".equals(queryType)){//条件查找
        	String query = (String) getRequest().getParameter("officeName");
        	page = useOfficeService.queryPageList(pageList,query);
        	//officeList = (PageDTO<TSaveoffice>) saveofficeService.queryLikeSql(pageList, query);
        }else{//无条件的初始页面
        	page = useOfficeService.queryPageList(pageList);
        }
        list = page.getContent(); //useOfficeList
        long total = page.getTotalElements();
        //list-->volist
        for(UseOffice po:list){
        	Integer id = po.getId();
        	String usename = po.getUser().getName();
        	String officename = po.getOffice().getName();
        	Integer count = po.getCount();
        	String unit = po.getOffice().getUnit();
        	String date = dateToString(po.getDate(),"yyyy-MM-dd HH:mm:ss");
        	useOfficeVo = new UseOfficeVo(id,usename,officename,count,unit,date);
        	volist.add(useOfficeVo);
        	//System.out.println(list.get(0).getUser().getName());
        }
        jsonMap.put("total", total);//total键 存放总记录数，必须的
        jsonMap.put("rows", volist);//rows键 存放每页记录 list
        return "useofficesList";
	}
	
	/**
	 * 办公用品领用登记
	 * @throws Exception 
	 */
	@Action("useoffice_addUseOffice")
	public void addUseOffice() throws Exception{
		Integer userId = useOfficeDto.getUserId();
		Integer officeId = useOfficeDto.getOfficeId();
		Integer count = useOfficeDto.getCount();
		String time = useOfficeDto.getTime();
		//根据userId查询出用户
		User user = userService.findById(userId);
		//根据officeId查询出办公用品
		Office office = officeService.getOfficeById(officeId);
		//相应的office的数量减少count
		Integer beforeCount = office.getCount();
		Integer afterCount = beforeCount - count;
		office.setCount(afterCount);
		officeService.updateOffice(office);
		//time(String) ->time(Date)
		Date date = stringToDate(time, "yyyy-MM-dd HH:mm:ss");
		//new UseOffice
		UseOffice useOffice = new UseOffice();
		useOffice.setUser(user);
		useOffice.setOffice(office);
		useOffice.setCount(count);
		useOffice.setDate(date);
		//执行添加
		useOfficeService.addUseOffice(useOffice);
		getResponse().getWriter().write("true");
	}
}
